package org.lql.algo.codecrush.week009.question.type.II;

import org.lql.algo.common.Node;
import org.lql.algo.common.Pair;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author: liangqinglong
 * @date: 2025-08-20 16:55
 * @description: 429. N 叉树的层序遍历 <a href="https://leetcode.cn/problems/n-ary-tree-level-order-traversal/description/">...</a>
 **/
public class LevelOrderN {

	public List<List<Integer>> levelOrder(Node root) {
		Queue<Pair<Node, Integer>> queue = new LinkedList<>();
		List<List<Integer>> ans = new ArrayList<>();
		if (root == null) {
			return ans;
		}
		queue.add(new Pair<>(root, 0));
		while (!queue.isEmpty()) {
			Node node = queue.peek().getKey();
			Integer depth = queue.poll().getValue();
			if (depth >= ans.size()) {
				ans.add(new ArrayList<>());
			}
			ans.get(depth).add(node.val);
			for (Node child : node.children) {
				queue.add(new Pair<>(child, depth + 1));
			}
		}
		return ans;
	}

	public static void main(String[] args) {
		LevelOrderN levelOrderN = new LevelOrderN();
		Node node1 = new Node(1, new ArrayList<>());
		Node node2 = new Node(3, new ArrayList<>());
		Node node3 = new Node(2, new ArrayList<>());
		Node node4 = new Node(4, new ArrayList<>());
		Node node5 = new Node(5, new ArrayList<>());
		Node node6 = new Node(6, new ArrayList<>());
		node3.children.add(node5);
		node3.children.add(node6);
		node1.children.add(node2);
		node1.children.add(node3);
		node1.children.add(node4);
		System.out.println(levelOrderN.levelOrder(node1));
	}
}
